const axios = require('axios')
const QS = require('qs')
axios.defaults.timeout = 15000
axios.defaults.withCredentials = true

const down = (url, params) => {
  let headers = {
    headers: {
      'Authorization': 'JWT ' + window.sessionStorage.getItem('user.token'),
      //'Content-Type': 'application/x-www-form-urlencoded'
    }
  };
  document.cookie = 'csrftoken=' + window.sessionStorage.getItem('user.token');

  return new Promise((resolve, reject) => {
    console.log(`start post to ${url}`, params)
    axios
      .post(url, params, headers)
      .then(res => {
        resolve(res)
      })
      .catch(err => {
        console.error(err)
        if (err.response) {
          reject(new Error('服务器内部错误'))
        } else {
          reject(err)
        }
      })
  })
}

const post = (url, params) => {
  let headers = {
    headers: {
      'Authorization': 'JWT ' + window.sessionStorage.getItem('user.token'),
      //'Content-Type': 'application/x-www-form-urlencoded'
    }
  };
  document.cookie = 'csrftoken=' + window.sessionStorage.getItem('user.token');

  return new Promise((resolve, reject) => {
    console.log(`start post to ${url}`, params)
    axios
      .post(url, params, headers)
      .then(res => {
        resolve(res.data)
      })
      .catch(err => {
        console.error(err)
        if (err.response) {
          reject(new Error('服务器内部错误'))
        } else {
          reject(err)
        }
      })
  })
}

const get = (url, params) => {
  let headers = {
    'Authorization': 'JWT ' + window.sessionStorage.getItem('user.token')
  };
  document.cookie = 'csrftoken=' + window.sessionStorage.getItem('user.token');

  return new Promise((resolve, reject) => {
    console.log(`start post to ${url}`, params, headers)
    axios
      .get(url, {params: params, headers: headers})
      .then(res => {
        resolve(res.data)
      })
      .catch(err => {
        console.error(err)
        if (err.response) {
          reject(new Error('服务器内部错误'))
        } else {
          reject(err)
        }
      })
  })
}

const del = (url, params) => {
  let headers = {
    'Authorization': 'JWT ' + window.sessionStorage.getItem('user.token')
  };
  document.cookie = 'csrftoken=' + window.sessionStorage.getItem('user.token');

  return new Promise((resolve, reject) => {
    console.log(`start post to ${url}`, params, headers)
    axios
      .delete(url, {params: params, headers: headers})
      .then(res => {
        resolve(res.data)
      })
      .catch(err => {
        console.error(err)
        if (err.response) {
          reject(new Error('服务器内部错误'))
        } else {
          reject(err)
        }
      })
  })
}

const put = (url, params) => {
  let headers = {
    'Authorization': 'JWT ' + window.sessionStorage.getItem('user.token')
  };
  document.cookie = 'csrftoken=' + window.sessionStorage.getItem('user.token');

  return new Promise((resolve, reject) => {
    console.log(`start post to ${url}`, params, headers)
    axios
      .put(url,  params, {headers: headers})
      .then(res => {
        resolve(res.data)
      })
      .catch(err => {
        console.error(err)
        if (err.response) {
          reject(new Error('服务器内部错误'))
        } else {
          reject(err)
        }
      })
  })
}

global.network = {post}
global.download = {down}
global.getdata = {get}
global.deletedata = {del}
global.updatedata = {put}